import 'package:flutter_magic_planet/model/video.dart';
import 'package:flutter_magic_planet/model/video_chapter.dart';

import 'request.dart';

class APIVideoPingcc {
  static const ID = 'api-pingcc-cn';

  /// https://easydoc.net/doc/45910076/WP3yrgI7/JS4OL8uC
  static const URL_PING_CC = 'http://api.pingcc.cn';

  /// 视频搜索 http://api.pingcc.cn/video/search/{option}/{key}/{from}/{size}
  static const String VIDEO_SEARCH = '/video/search/';

  /// 视频章节 http://api.pingcc.cn/videoChapter/search/{videoId}
  static const String VIDEO_CHAPTER_SEARCH = '/videoChapter/search/';

  /// 视频搜索
  static Future<List<Video>> getVideos(
      {required String option, required String key}) async {
    final Map response = await Request.get(
        action: '${URL_PING_CC + VIDEO_SEARCH}$option/$key/1/99');
    if (response['code'] == 0) {
      List data = response['data']['data'];
      return data.map((e) => Video.fromJson(e)).toList();
    }
    return [];
  }

  /// 根据ID获取视频章节列表
  static Future<List<VideoChapter>> getVideoChapters(
      {required String id}) async {
    final Map response =
        await Request.get(action: URL_PING_CC + VIDEO_CHAPTER_SEARCH + id);
    if (response['code'] == 0) {
      List data = response['data']['data'];
      return data.map((e) => VideoChapter.fromJson(e)).toList();
    }
    return [];
  }
}
